package com.sgcc.pda.data.dao.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import com.sgcc.pda.data.dao.ExceptionDao;
import com.sgcc.pda.greendao.dao.DBCommit;
import com.sgcc.pda.greendao.entity.ExceptionInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * File description.
 *
 * @author 王沛栋
 * @data 2018/1/19 13:46
 */
@SuppressLint("SimpleDateFormat")
public class ExceptionDaoImpl implements ExceptionDao {

    private String versionName, versionCode;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 收集设备参数信息
     *
     * @param ctx
     */
    private void collectDeviceInfo(Context ctx) {
        if (TextUtils.isEmpty(versionName)) {
            try {
                PackageManager pm = ctx.getPackageManager();
                PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
                if (pi != null) {
                    versionName = pi.versionName == null ? "null" : pi.versionName;
                    versionCode = pi.versionCode + "";
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public boolean saveExceptionInfo(Context context, String userid, String detail, String data, String status, String etype) {
        collectDeviceInfo(context);
        ExceptionInfo info = new ExceptionInfo();
        info.setDev_info(Build.MODEL);
        info.setOs_version("android" + Build.VERSION.RELEASE);
        info.setApp_version(versionName);
        info.setUserid(userid);
        info.setCtime(simpleDateFormat.format(new Date(System.currentTimeMillis())));
        info.setDetail(detail);
        info.setData(data);
        info.setStatus(status);
        info.setEtype(etype);

        return DBCommit.getExceptionInfoDBManager(context).insertObject(info);
    }

    public String getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }

}
